import requests
import time
import base64
from pyDes import *


# 使用说明: 按需修改这些参数后直接run即可无需改代码
HOST = 'http://192.168.75.111:8889'
REQUEST_URL = '/userLogin/login'
TYPE = 'GET'
PARAMS = {'mobile': '13532244566', 'password': 'jklQbMcTv7FAAXmRtxTlYg=='}
ADMIN_LOGIN = False
USER_LOGIN = False


HEADERS = {
    "method": "POST",
    "scheme": "https",
    "version": "HTTP/1.1",
    "accept": "*/*",
    "accept-encoding": "gzip, deflate",
    "accept-language": "en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4",
    "content-type": "application/x-www-form-urlencoded;charset=UTF-8",
    "User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.6) ",
    "dnt": "1",
    "faces-request": "partial/ajax"
}


def des_encrypt(key, content):
    k = des(key.encode(), ECB, b"\0\0\0\0\0\0\0\0", padmode=PAD_PKCS5)
    des_bytes = k.encrypt(content)
    return base64.b64encode(des_bytes).decode('ascii')


def admin_login():
    account_data = {
        "userName": "admin",
        "password": "admin123"
    }
    conn = requests.session()
    conn.post("%s/login/login" % HOST, data=account_data, headers=HEADERS)
    return conn


def user_login():
    account_data = {
        "userName": "13532244566",
        "password": "jklQbMcTv7FAAXmRtxTlYg=="
    }
    conn = requests.session()
    conn.post("%s/userLogin/login" % HOST, data=account_data, headers=HEADERS)
    return conn


def raw_connect():
    return requests.session()


if __name__ == '__main__':
    if ADMIN_LOGIN:
        conn = admin_login()
    elif USER_LOGIN:
        conn = user_login()
    else:
        conn = raw_connect()
    time.sleep(1)
    if TYPE == 'GET':
        response = conn.get("%s%s" % (HOST, REQUEST_URL), params=PARAMS)
    else:
        response = conn.post("%s%s" % (HOST, REQUEST_URL), params=PARAMS)
    print(response.text)